ARC596 - Tutorial 3 - Unity AR App

ARC596 - Tutorial 3 - Unity AR App

Requirements

  1. Rhinoceros 7
  2. Github Desktop
  3. Anaconda
  4. Unity 2022.3.3f1
    *Note: Android SDK and Java JDK (when developing for Android) - have to be ticked in the installation modules when installing Unity.*
    

Dependencies

  1. COMPAS
  2. COMPAS Fab - Fabrication Library for Robots
  3. COMPAS Eve - Communication
  4. Vuforia
  5. ROS#

Create an AR Test App

Note: Be sure that the AR dependencies are installed per the prior section of this tutorial.

Configure an AR session and add AR Foundation components

A scene needs an AR session to enable AR processes, such as motion tracking, environmental understanding, and lighting estimation. You will need the following game objects to support an AR session:

  1. Before adding the new game objects, delete the default Main Camera. It will be replaced by a new Camera Offset in the XR Origin.

  2. Add the new AR game objects to your scene: right-click the Hierarchy pane and select XR. Add a new AR Session and a new XR Origin (Mobile AR) game object.

What is a Session?

All AR processes, such as motion tracking, environmental understanding, and lighting estimation, happen inside an ARCore session. ARSession is the main entry point to the ARCore API. It manages the AR system state and handles the session lifecycle, allowing the app to create, configure, start, or stop a session. Most importantly, it enables the app to receive frames that allow access to the camera image and device pose.

Detect planes in the real world

Add an ARPlane Manager Component and place an AR Plane Prefab

An ARPlaneManager detects ARPlanes and creates, updates, and removes game objects when the device’s understanding of the environment changes.

We will need this component later. It helps obtain information of the raycasts deriving from the user input on the screen.

This XR GameObject contains scripts that generate and visualize the planes detected by the AR device, using the AR Plane manager script we created in the previous step.

Note: In short, Unity’s Prefab system allows you to create, configure, and store a GameObject complete with all its components, property values, and child GameObjects as a reusable Asset. The Prefab Asset acts as a template from which you can create new Prefab instances in the Scene.
→ More info about prefabs

→ Notice that the AR Default Plane becomes blue in the Hierarchy. This is because the process of creating the Prefab Asset also turns the original GameObject into a Prefab instance. Every change happening in the prefab, will happen in the instance as well.

You should now see the AR Default Plane Prefab placed as the Plane Prefab.

We can now visualize the AR detected planes in our device.

Let’s build the app and see if it works!

Build the AR App to test the AR Plane detection

Configure Build and Project Settings in Unity

  1. Select Android in build settings

    • Go to File > Build Settings

    • Switch to the Android Platform - Go to player settings

    • If Unity requires you to download Android support in order to switch platforms, follow the link and install the required dependency.

    • Go to Player > Other Settings > Rendering

    • Make sure “Auto Graphics API” is unchecked. Change Color Space to “Linear”

    • Remove Vulkan from under Graphics APIs

    • Go to Player > Other Settings > Package Name. Create a unique app ID using a Java package name format. For example, use com.Princeton.AR
    • Go to Player > Other Settings > Minimum API Level. Select Android 7.0 ‘Nougat’ (API Level 24) or higher (For AR Optional apps, the Minimum API level is 14.)
    • Go to Player > Other Settings > Scripting Backend. Select IL2CPP instead of Mono.
    • Go to Player > Other Settings > Target Architectures. To meet the Google Play 64-bit requirement, enable ARM64 (64-bit ARM). Leave ARMv7 (32-bit ARM) enabled to support 32-bit devices

  2. Configure Project Settings

    • Open Edit > Project Settings… and click on the XR Plug-in Management section. In the Android tab, enable ARCore.

  3. Configure your phone to run the app

    • Plug your Android AR Phone into your computer, using the USB cable.
    • Make sure that your computer recognizes the device. You can check this in This PC>Devices and drives

    • If you don’t see it, try having the device unlocked when you plug it, and if USB options popup, select Use USB to: Transfer files on your phone screen.

    • Go to File>Build Settings
    • Respectively, you should be able to see your device detected in the Run Device drop-down Menu. You can click Refresh if you don’t see it right away.

  4. Build and run the app

    • Click Build and Run. Select a folder and name your application as desired. This might take a bit of time. Keep the device unlocked, so that the application runs right away.

    • Move the phone slowly up and down, left and right, to start the detection.

    • After some time, you should be able to see this semi-transparent yellow material with an outline, indicating the scanned AR Planes.

Import a file into Unity from Rhino

Export file from Rhino

In Rhino, open your desired file. In our case it is a house form.

Note: Make sure you file is set to meters in Rhino.

Note: This step will help us have our model correctly oriented in Unity.

Note: Check that your file is created correctly in the folder.

Import file in Unity

First, we will learn how to import our model correctly into Unity, adjust its scale and position it correctly. In this example, Rhinoceros is a common CAD software, but you can use any preferred modeling software. Be sure to structure your model according to the materials you want to apply on it later. Unity understands a variety of file types, you can use OBJ and export each layer as a separate OBJ file.

Back in the existing Unity App, go to the Prefab folder we created before in the Assets (Project Window).

Note: Unity Projects are actually folders that contain other folders. So you can have full access on Prefabs, scripts, objects. When you put something in this folder, it updates in Unity as well.

Note: Alternatively, you can make the new folder inside Unity and drag and drop both of the files.

Create Prefab with imported Model

Note: Don’t forget to Save your Project from time to time! File > Save

Now that the scale of the object is correct, we can apply a Material of our choice. Let’s go back to Assets and make another empty Folder called Materials. Here we will be organizing our materials throughout the development.

Tip: You can rename an existing folder by clicking on it once, and pressing F2 on your keyboard.

Instantiate object

Now we can start the scripting base! The goal for today is to “Tap and Instantiate” the 3D object in our App, on the detected AR Planes.

Import the Instantiator Script

Note: Always make sure that the Transform Values are set to Zero when creating a new GameObject.

Overview of the code

Do these variables seem familiar? They are the ones that appear on the Inspector!

We can choose what kind of GameObject we need.

E.g. For the Camera, we only need the “Transform” component, to get information about its position and rotation in space.

Let’s drag and drop the objects we need.

Note: Make sure the Transform is set to 0,0,0 in the Inspector for the new object. Likewise, make sure it is set to zero for the AR Session and XR Origin GameObjects.

Build App

Misc Information

  1. MonoBehaviour is the base class from which every Unity script object derives its properties and methods (functions).
  2. A class enables you to create your custom types by grouping variables of other types, methods, and events.
  3. The API acronym stands for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other.
  4. Software Development Kits (SDKs) allow development for a variety of platforms. They contain the compiler and debugger and usually software frameworks. For example, Unity has the Android SDK, which allows you to build for the Android operating system.
  5. Tags help you link GameObjects to variables in your scripts.